import React from 'react';
import { fetchFavoriteArticle } from '../actions/article';
import { connect } from 'react-redux';
import Articles from '../components/articles';
import Loadmore from '../components/loadmore';
import { Link } from 'react-router';
import NormalHeaderBar from '../components/NormalHeaderBar';

import { loadMoreArticle, initArticles } from '../actions/home';

class ConferenceList extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.initArticles();
        this.props.loadMoreArticle(this.props.params.channel, 1, 0);
    }

    handleLoad(page) {
        this.props.loadMoreArticle(this.props.params.channel, page, 0);
        hashHistory.push('conference/list/'+this.props.params.channel+'/'+ page);
    }

    render() {
        let articles = [];
        let articleDiv = (<div></div>);
        if (this.props.articles !== undefined && this.props.articles.length > 0) {
            articles = this.props.articles;
            articleDiv = (<Articles articles={articles}/>)
        } 
        let title = "";
        if (sessionStorage.channels != undefined && sessionStorage.channels != "undefined") {
            let arr = JSON.parse(sessionStorage.channels);
            arr.forEach(function(item){
                if (item.ID == this.props.params.channel)
                    title = item.Title;
            }.bind(this));
        }
        return (
            <div>
                <NormalHeaderBar title={title}/>
                { articleDiv }
                { articles.length > 0 && <Loadmore page={this.props.page} defaultPageSize={articles.length} pageCount={(articles.length * this.props.pageCount)} handleLoad={this.handleLoad.bind(this)} pageChange={this.handleLoad.bind(this)}/>}
            </div>
        )
    }
}

function mapStateToProps(state){
    return { 
        articles: state.home.articles,
        page: state.home.page,
        pageCount: state.home.pageCount,
    };
}

export default connect(mapStateToProps, { loadMoreArticle, initArticles })(ConferenceList);